package com.shallowUniverse.process.mapper.provider;


import com.shallowUniverse.process.context.sql.SqlSentenceUtil;
import com.shallowUniverse.process.entity.Approval;
import com.shallowUniverse.process.dto.TableFieldCondition;

import java.util.Map;

/**
 * @author gs
 * @date create in 2021/1/17 14:31
 */
public class ApprovalProvider {

    public String save(Approval approval) {
        return SqlSentenceUtil.generateSentence("insertApproval");
    }

    public String selectTableData(TableFieldCondition tableFieldCondition) {
        return SqlSentenceUtil.generateSentence("selectTableData");
    }

    public String selectById(Long id) {
        return SqlSentenceUtil.generateSentence("selectApprovalById");
    }

    public String selectList(Approval approval) {
        return joinConditions("selectApprovals", approval, true);
    }

    public String selectByRole(String role, Long appoint, Long deptId, Approval approval) {
        return joinConditions("selectApprovalByRole", approval, false);
    }

    public String update(Approval approval) {
        return SqlSentenceUtil.generateSentence("updateApprovalById");
    }

    public String selectByUser(Long id, Approval approval) {
        return joinConditions("selectApprovalByUser", approval, false);
    }

    public String selectTableFields(String target) {
        return SqlSentenceUtil.generateSentence("selectTableFields");
    }

    public String deleteLogic(String id) {
        return SqlSentenceUtil.generateSentence("deleteApprovalLogic");
    }

    public String delete(String id) {
        return SqlSentenceUtil.generateSentence("deleteApproval");
    }

    public String updateApprovePerson(Map<String, String> params) {
        return SqlSentenceUtil.generateSentence("updateApprovalAppointById");
    }

    public String selectNotCompletedByProcess() {
        return SqlSentenceUtil.generateSentence("selectApprovalByProcessNotCompleted");
    }

    public String selectNotCompletedByIds() {
        return SqlSentenceUtil.generateSentence("selectApprovalByIdsNotCompleted");
    }

    public String selectNotCompletedByProcessNode() {
        return SqlSentenceUtil.generateSentence("selectApprovalByProcessNodeNotCompleted");
    }

    private String joinConditions(String sqlId, Approval approval, boolean queryAll) {
        //查询字段和条件字符
        String[] entityFields = null;
        String[] sqlFields = null;
        String[] signs = null;
        if (queryAll) {
            entityFields = new String[]{"userId", "startDate", "endDate", "processId", "status", "target"};
            sqlFields = new String[]{"pa.user_id", "pa.create_date", "pa.create_date", "pa.process_id", "pa.status", "pa.target"};
            signs = new String[]{"=", ">=", "<=", "=", "=", "="};
        } else {
            entityFields = new String[]{"startDate", "endDate", "processId", "status", "target"};
            sqlFields = new String[]{"pa.create_date", "pa.create_date", "pa.process_id", "pa.status", "pa.target"};
            signs = new String[]{">=", "<=", "=", "=", "="};
        }
        //调用方法拿到sql
        return SqlSentenceUtil.generateSentenceWithFieldConditions(sqlId, approval, entityFields, sqlFields, signs);
    }
}
